class Media < ActiveRecord::Base
acts_as_taggable
validates_presence_of :title,:text
belongs_to :category
belongs_to :type
belongs_to :user
has_many :comments, :dependent => :destroy

def self.setpermalink(title)
  st="media/"+Time.now.strftime("%Y").to_s+"/"+Time.now.strftime("%m").to_s+"/"+Time.now.strftime("%d").to_s+"/"+Media.textual(title)
  return st
end

def self.find_comments(id)
  @result=Comment.find_all_by_media_id(id)
  return @result
end

def self.validhtml(phrase)
	@newphrase=""
	for i in 0..phrase.size+1
		if (phrase[i].to_i < 65) or (phrase[i].to_i > 126)
			@newphrase+="&#"+phrase[i].to_s+";"
		else
			@newphrase+=phrase[i].to_i.chr
		end
	end
	return @newphrase
end

def self.textual(st)
   foo=st.gsub(/[áàâäãÃÄÂÀ]/,'a')
   foo=foo.gsub(/[éèêëËÉÈÊ]/,'e')
   foo=foo.gsub(/[íìîïIÎÌ]/,'i')
   foo=foo.gsub(/[óòôöõÕÖÔÒ]/,'o')
   foo=foo.gsub(/[úùûüUÛÙ]/,'u')
   foo=foo.gsub(/[ÿŷỳ]/,'y')
   foo=foo.gsub(/[œ]/,'oe')
   foo=foo.gsub(/[ĉ]/,'c')
   foo=foo.gsub(/[ß]/,'ss')
   foo=foo.sub(/[\s\'\"\\\/\?\.\=\+\&\%]$/,'')
   foo=foo.gsub(/[\s\'\"\\\/\?\.\=\+\&\%]/,'_')
   foo=foo.gsub(/[\s#,;:+]/,'_')
   return foo
end

end
